<?php

namespace app\common\model\Subject;

use think\Model;

// 软删除的模型
use traits\model\SoftDelete;

class Subject extends Model
{
    //继承软删除
    use SoftDelete;

    //模型对应的是哪张表
    protected $table = "pre_subject";

    //指定一个自动设置的时间字段
    //开启自动写入
    protected $autoWriteTimestamp = true; 

    //设置字段的名字
    protected $createTime = "createtime"; //插入的时候设置的字段名

    //禁止 写入的时间字段
    protected $updateTime = false;

    // 软删除的字段
    protected $deleteTime = 'deletetime';

     // 追加属性
     protected $append = [
       'likes_text',
       'thumbs_cdn', // 课程封面
       'content_text', // 过虑内容标签
    ];

    public function getThumbsCdnAttr($value, $data)
    {
        $thumbs = empty($data['thumbs']) ? '/assets/img/coures.jpg' : $data['thumbs'];
        return $thumbs;
    }

     //获取器方法
    public function getLikesTextAttr($value, $data)
    {
        $likes = trim($data['likes']);
         //字符串转化为数组
         $likes = explode(',', $data['likes']);
         $likes = array_filter($likes);
         return count($likes);
    }

    
    // 过滤内容标签
    public function getContentTextAttr($value, $data)
    {
        
        $content = empty($data['content']) ?  '' : preg_replace("/<.+?>|&.+?;/","",$data['content']);
        return $content;
        
    }


     public function category()
    {
        return $this->belongsTo('Category',  'cateid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

  
}
